#include "iic.h"

#define EEPROM_IIC_ADDR 0xA0

void eeprom_wait_idle();

char eeprom_find_device() 
{
	char res = 0;
	iic_init();
	iic_start();
	res = iic_write_byte(EEPROM_IIC_ADDR);
	iic_stop();
	return res;
}

void eeprom_wait_idle() 
{
	unsigned char ack = 0xFF;
	do {
		iic_start();
		ack = iic_write_byte(EEPROM_IIC_ADDR);
	} while(ack != 0);
}

void eeprom_write_page(unsigned char rom_addr, unsigned char* content) 
{
	char i= 0;
	iic_init();
	iic_start();
	iic_write_byte(EEPROM_IIC_ADDR);
	iic_write_byte(rom_addr);
	for( ;i<8;++i) {
		iic_write_byte(*content);
		content += 1;
	}
	iic_stop();
	eeprom_wait_idle();
}

unsigned char eeprom_write_data(unsigned char rom_addr, unsigned char content) 
{
	unsigned char res = 0;
	iic_init();
	iic_start();
	res |= iic_write_byte(EEPROM_IIC_ADDR);
	res |= iic_write_byte(rom_addr);
	res |= iic_write_byte(content);
	iic_stop();
	eeprom_wait_idle();
	return res;
} 

unsigned char eeprom_read_data(unsigned char rom_addr) 
{
	unsigned char result = 0;
	iic_init();
	iic_start();
	iic_write_byte(EEPROM_IIC_ADDR);
	iic_write_byte(rom_addr);
	iic_start();
	iic_write_byte(EEPROM_IIC_ADDR | 0x01);
	result = iic_read_byte(1);
	iic_stop();
	return result;
}